using System;
using System.Linq;
using Bubbles;
using Data;
using Locomotion;
using PrimeTween;
using UnityEngine;

namespace Controls
{
    public class ProcessController : MonoBehaviour
    {
        [SerializeField] private Bubble firstBubble;
        [SerializeField] private MessageSpawner messageSpawner;
        [SerializeField] private JumpCharacter character;
        [SerializeField] private float jumpInterval = 1f;


        private void Start()
        {
            var firstBubbleInfo = ScriptableObjectHelper.GetFixedBubbleData(true).listFixedBubbleInfo[0];
            var firstBubbleText = firstBubbleInfo.content;
            if (firstBubbleInfo.emojiAtTail != null && firstBubbleInfo.emojiAtTail.Length > 0)
            {
                firstBubbleText += firstBubbleInfo.emojiAtTail;
            }
            firstBubble.SetText(firstBubbleText);
        }

        private void OnEnable()
        {
            GroundDetector.AnyCharacterGroundedChanged += GroundDetectorOnAnyCharacterGroundedChanged;
        }

        private void OnDisable()
        {
            GroundDetector.AnyCharacterGroundedChanged -= GroundDetectorOnAnyCharacterGroundedChanged;
        }

        private void GroundDetectorOnAnyCharacterGroundedChanged(GroundDetector.GroundChangedEventArgs groundChangedEventArgs)
        {
            if (!groundChangedEventArgs.GroundDetector.IsGrounded)
            {
                return;
            }

            Tween.Delay(jumpInterval, StartJump);
        }

        private void StartJump()
        {
            messageSpawner.DisablePreviousMessages();
            character.Jump();
        }
    }
}